package com.shop.cereshop.app.controller.tencent.im;

import com.shop.cereshop.app.tencent.im.model.redpacket.RedPacketVO;
import com.shop.cereshop.app.tencent.im.model.redpacket.CreateRedPacketParam;
import com.shop.cereshop.app.tencent.im.model.redpacket.ReceiveRedPacketParam;
import com.shop.cereshop.app.tencent.im.model.redpacket.RedPacketDetailVO;
import com.shop.cereshop.app.tencent.im.model.redpacket.SendRedPacketParam;
import com.shop.cereshop.app.tencent.im.service.RedPacketService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @description: 腾讯IM红包接口
 * @author zdd
 * @date 2025/9/3 14:36
 * @version 1.0
 */
@RestController
@RequestMapping("/tim/red-packet")
@Slf4j(topic = "TimRedPacketController")
@Api(value = "腾讯IM 红包接口", tags = "腾讯IM 红包接口")
public class TimRedPacketController {

    @Resource
    private RedPacketService redPacketService;

    @ApiOperation(value = "创建红包")
    @PostMapping(value = "/create")
    public Result<RedPacketVO> create(HttpServletRequest request, @Validated @RequestBody CreateRedPacketParam param) {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        param.setSenderId(String.valueOf(user.getBuyerUserId()));
        param.setSenderName(user.getName());
        return redPacketService.create(param);
    }

    @ApiOperation(value = "发送红包")
    @PostMapping("/send")
    public Result<Void> send(HttpServletRequest request, @Validated @RequestBody SendRedPacketParam param) {
        String ipAddress = IpUtils.getClientIpAddress(request);
        param.setIpAddress(ipAddress);
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        param.setSenderHeadImage(user.getHeadImage());
        return redPacketService.send(param);
    }

    @ApiOperation(value = "领取红包")
    @PostMapping("/receive")
    public Result<Object> receive(HttpServletRequest request, @Validated @RequestBody ReceiveRedPacketParam param) {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        param.setReceiverId(String.valueOf(user.getBuyerUserId()));
        param.setReceiverName(user.getName());
        return redPacketService.receive(param);
    }

    @ApiOperation(value = "查询红包详情")
    @GetMapping("/detail/{packetId}")
    public Result<RedPacketDetailVO> detail(@PathVariable String packetId) {
        return redPacketService.detail(packetId);
    }

}
